import axios from 'axios'
import { Message } from 'element-ui'

const URL = `http://${process.env.APP_IPV4}:${process.env.APP_PORT}`

const service = axios.create({
  baseURL: URL,
  timeout: 5000
})

service.interceptors.response.use(
  ({ status, statusText, data }) => {
    if (status !== 200) {
      return Promise.reject(new Error(statusText || 'Error'))
    }
    if (data.code !== 200) {
      return Promise.reject(new Error(data.message || 'Error'))
    }
    return data.data
  },
  error => {
    Message({
      message: error.message,
      type: 'error',
      duration: 5 * 1000
    })
    return Promise.reject(error)
  }
)

export default service
